const playwright = require('playwright');
const chalk = require('chalk');
const { chromium } = playwright;

let page = null;
async function initBrowser(locale) {
  try {
    browser = await chromium.launch({ headless: false });
    context = await browser.newContext();
    context.clearCookies();

    page = await context.newPage();
    await page.context().clearCookies();
    page.setDefaultTimeout(600000);

    await page.goto('https://fanyi.baidu.com/#zh/en/');
    if (await page.$('.f9WVaz_4')) {
      await page.click('.f9WVaz_4');
    }
    // 百度翻译最近加广告啦，需要先关闭一下广告
    if (await page.$('.K_1jNlb8')) {
      await page.click('.K_1jNlb8');
    }
    // 百度翻译最近加广告啦，需要先关闭一下广告
    if (await page.$('.YgeucjCj')) {
      await page.click('.YgeucjCj');
    }
    // 切换到传统翻译 不需要登录
    if (await page.$('.ChQuWLoz')) {
      await page.click('.ChQuWLoz');
    }

   /*  let p = Promise.resolve();
    const word = '需要翻译';
    p = page.type('.kXQpwTof', word, {
      delay: 0,
    });
    // await p;

    let result;
    await page.waitForSelector('.nbyIWSn1 span', { timeout: 20000 });
    result = await page.$eval('.nbyIWSn1 span', (el) => el.textContent);
    console.log('result', result); */

    return true;
  } catch (error) {
    console.log(chalk.red(`初始化浏览器失败, 错误: ${error}}`));
    return false;
  }
}

// 设置翻译语言
async function setCurrentLocal(locale) {
  // textContent;
  const tran_a = await page.evaluate(
    () => document.querySelector('.qFqhz5ZO :nth-child(1) span')?.textContent,
  );
  if (tran_a !== '中文(简体)') {
    await page.click('.qFqhz5ZO :nth-child(1)');
    await page.click('.lang-table div  .sRoBHEPq:last-child .tL_8ZLXu:first-child');
    await page.waitForTimeout(1000);
  }

  const tran_b = await page.evaluate(
    () => document.querySelector('.qFqhz5ZO :nth-child(3) span')?.textContent,
  );
}

// 翻译

async function checkCurrentLocal(locale) {
  let str = await page.evaluate(
    () => document.querySelector('.qFqhz5ZO :nth-child(1) span ')?.textContent,
  );

  if (str != '中文(简体)') {
    await page.click('.qFqhz5ZO :nth-child(1)');
    await page.click('.lang-table div  .sRoBHEPq:last-child .tL_8ZLXu:first-child');
    await page.waitForTimeout(1000);
  }

  str = await page.evaluate(
    () => document.querySelector('.qFqhz5ZO :nth-child(3) span ')?.textContent,
  );

  const curStr = localeDict[locale];
  if (curStr != str) {
    await page.click('.qFqhz5ZO :nth-child(3)');
    switch (locale) {
      case 'yue':
        await page.click('.lang-table div  .sRoBHEPq:last-child .tL_8ZLXu:nth-child(2)');
        break;
      case 'en':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(21) .tL_8ZLXu:nth-child(1)');
        break;
      case 'de':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(4) .tL_8ZLXu:nth-child(1)');
        break;
      case 'es':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(20) .tL_8ZLXu:nth-child(1)');
        break;
      case 'in':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(21) .tL_8ZLXu:nth-child(4)');
        break;
      case 'ja':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(16) .tL_8ZLXu:nth-child(1)');
        break;
      case 'pt':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(14) .tL_8ZLXu:nth-child(1)');
        break;
      case 'tr':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(18) .tL_8ZLXu:nth-child(2)');
        break;
      case 'vi':
        await page.click('.lang-table div  .sRoBHEPq:nth-child(21) .tL_8ZLXu:nth-child(2)');
        break;
    }
    await page.waitForTimeout(1000);
  }
}

async function main(params) {
  let ret = await initBrowser();
  setCurrentLocal('en');
}

main();
